<?php

App::uses('AppController', 'Controller');

class SystemController extends AppController {
    //put your code here
    
    public $uses = array('User','Country','Currency','OrganisationSetting');
    
    public function beforeFilter() {
        parent::beforeFilter();
        
        $this->autoRender = false;
    }
    
    public function category_settings() {
        
        $categories = ClassRegistry::init('MemberCategorySetting')->find('all', array(
            'order' => array('MemberCategorySetting.position'),
            'recursive' => -1
        ));
        
        return $this->_response($categories);
    }
    
    public function modules() {
        $modules = ClassRegistry::init('Module')->find('all', array(
            'contain' => array(
                'ModuleMenu' => array(
                    'conditions' => array('ModuleMenu.active' => 1),
                    'order' => array('ModuleMenu.position')
                )
            ),
            'conditions' => array('Module.active' => 1),
            'order' => array('Module.menu_position')
        ));
        
        return $this->_response($modules);
    }


    public function getCountries(){

        $response = $this->Country->getCountries();

        return $this->_response($response);
    }

    public function getCountry(){
        $rules = array('required' => 'country_id');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $response = $this->Country->getCountry($country_id);

        return $this->_response($response);
    }

    public function getCurrencies(){
        $response = $this->Currency->getCurrencies();

        return $this->_response($response);
    }

    public function getCurrency(){
        $rules = array('required' => 'currency_id');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $response = $this->Currency->getCurrency($currency_id);

        return $this->_response($response);
    }

    public function getDefaultCurrency(){
        $rules = array('required' =>'organisation_id');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $response = $this->OrganisationSetting->getSetting($organisation_id,'org_contribution_default_currency');

        return $this->_response($response);
    }


    public function setDefaultCurrency(){
        $rules = array('required' =>'organisation_id','default_currency');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $save = $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_default_currency',$default_currency);
        if($save){
            $response =  array('status' => 'success', 'message' => 'Default currency was set successfully');
        }
        else{
            $response =  array('status' => 'failed', 'message' => 'Cannot set default currency.');
        }

        return $this->_response($response);
    }


    public function getReceiptSettings(){
        $rules = array('required' =>'organisation_id');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $data = $this->OrganisationSetting->getSetting($organisation_id,'org_contribution_receipt_mode');
        if($data){
            $receipt_mode = $data['OrganisationSetting']['value'];
            if($receipt_mode == null){
                $receipt_mode = 'manual';
            }
        }
        else{
            $receipt_mode = 'manual';
        }

        $data = $this->OrganisationSetting->getSetting($organisation_id,'org_contribution_receipt_prefix');
        if($data){
            $receipt_prefix = $data['OrganisationSetting']['value'];
            if($receipt_prefix == null){
                $receipt_prefix = '';
            }
        }
        else{
            $receipt_prefix = '';
        }

        $data = $this->OrganisationSetting->getSetting($organisation_id,'org_contribution_receipt_postfix');
        if($data){
            $receipt_postfix = $data['OrganisationSetting']['value'];
            if($receipt_postfix == null){
                $receipt_postfix = '';
            }
        }
        else{
            $receipt_postfix = '';
        }

        $data = $this->OrganisationSetting->getSetting($organisation_id,'org_contribution_receipt_count');
        if($data){
            $receipt_counter = $data['OrganisationSetting']['value'];
            if($receipt_counter == null){
                $receipt_counter = 1;
            }
        }
        else{
            $receipt_counter = 1;
        }

        $response = array(
            'receipt_mode'=>$receipt_mode,
            'receipt_prefix'=>$receipt_prefix,
            'receipt_postfix'=>$receipt_postfix,
            'receipt_counter'=>$receipt_counter
        );

        return $this->_response($response);
    }


    public function setReceiptSettings(){
        $rules = array('required' =>'organisation_id','receipt_mode','receipt_prefix','receipt_postfix','receipt_counter');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $receipt_mode = trim($receipt_mode);
        $receipt_prefix = trim($receipt_prefix);
        $receipt_postfix = trim($receipt_postfix);
        $receipt_counter = trim($receipt_counter);


        $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_mode',$receipt_mode);
        $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_prefix',$receipt_prefix);
        $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_postfix',$receipt_postfix);
        $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_count',$receipt_counter);


        $response =  array('status' => 'success', 'message' => 'Receipt settings was saved successfully');

        return $this->_response($response);
    }


    public function setReceiptCounter(){
        $rules = array('required' =>'organisation_id','receipt_counter');

        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);

        $receipt_counter = trim($receipt_counter);
        if(is_numeric($receipt_counter)){

            $data = $this->OrganisationSetting->getSetting($organisation_id,'org_contribution_receipt_count');
            if($data){
                $old_receipt_counter = $data['OrganisationSetting']['value'];
                if($old_receipt_counter == null){
                    $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_count',$receipt_counter);
                    $response =  array('status' => 'success', 'message' => 'Receipt counter was saved successfully');
                }
                else{
                    $receipt_counter = intval($receipt_counter);
                    $old_receipt_counter = intval($old_receipt_counter);
                    if($receipt_counter > $old_receipt_counter){
                        $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_count',$receipt_counter);
                        $response =  array('status' => 'success', 'message' => 'Receipt counter was saved successfully');
                    }
                    else{
                        $response =  array('status' => 'failure', 'message' => 'Default Receipt counter is greater than receipt counter passed.');
                    }
                }
            }
            else{
                $this->OrganisationSetting->setSetting($organisation_id,'org_contribution_receipt_count',$receipt_counter);
                $response =  array('status' => 'success', 'message' => 'Receipt counter was saved successfully');
            }
        }
        else{
            $response =  array('status' => 'failure', 'message' => 'Receipt counter passed is not a valid number');
        }


        return $this->_response($response);
    }



}